package com.rhm.dataStructure.Leetcode;/**
 * @Author:759057893@qq.com Lyz
 * @Date: ${Date} 23:51
 * @Description:
 **/

import java.util.Scanner;
import java.util.Stack;

/**
 *@Author:759057893@qq.com Lyz
 *@Date: 2020/2/21 23:51
 *@Description:
 **/

/*
leetcode20  有效的括号
 */
public class Code20 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String s = sc.nextLine();
            System.out.println(isTrueOrFalse(s));
        }
    }

 /*   public static boolean isValid(String s) {
        if(s.isEmpty()){
            return true;
        }
        Stack<Character> stack=new Stack<>();
        for(int i =0;i<s.length(); i++){
            char c = s.charAt(i);
            if(c=='(')
                stack.push(')');
            else if(c=='{')
                stack.push('}');
            else if(c=='[')
                stack.push(']');
            else if(stack.empty()||c!=stack.pop())
                return false;
        }
        return stack.empty();
    }*/



 /*   public static boolean isTrueOrFalse(String s){
        Stack<Character> stack = new Stack<> ();
        for(int i =0;i<s.length(); i++) {
            char c = s.charAt(i);
            switch(c){
                case '(':
                case '{':
                case '[':
                    stack.push(c);
                    break;
                case ')':
                case '}':
                case ']':
                    if(!stack.isEmpty()){
                        char r = stack.pop();
                        if(!((r=='('&&c==')')||(r=='{'&&c=='}')||(r=='['&&c==']'))){
                           return false;
                        }
                    }
                    break;
            }
        }
        return stack.isEmpty();
    }*/
    public static boolean isTrueOrFalse(String s){
        Stack<Character> stack = new Stack<> ();
        for(int i =0;i<s.length(); i++) {
            switch(s.charAt(i)){
                case '(': stack.push(s.charAt(i));break;
                case '{':stack.push(s.charAt(i));break;
                case '[':stack.push(s.charAt(i));break;
                case ')':
                    if(stack.isEmpty()){
                        return  false;
                    }else {
                        if(stack.peek() =='(') stack.pop();break;
                    }
                case '}':
                    if(stack.isEmpty()){
                        return  false;
                    }else {
                        if(stack.peek() =='{') stack.pop();break;
                    }
                case ']':
                    if(stack.isEmpty()){
                        return  false;
                    }else {
                        if(stack.peek() =='[') stack.pop();break;
                    }
            }
        }
       return stack.isEmpty();
    }
}
